package pe.com.mowa.siscobsms.datos;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Random;

import oracle.jdbc.OracleTypes;
import pe.com.mowa.siscobsms.bean.persona;

import com.sun.xml.internal.ws.org.objectweb.asm.Type;
public class conPersona {
	

	
	public void setPersona(persona per) throws IOException, ClassNotFoundException, Exception
	{
			CallableStatement cs;
			Connection cn;
			cs = null;
			int estado=0;
			
			try {
	    			System.out.print("BEGIN SET PERSONA");
					cn = new conexion().get_conection();
					String sentence ="{ ? = call \"siscobsms\".\"setPersona\"(?,?,?,?,?,?) }";
					cs = cn.prepareCall(sentence);
     		        cs.registerOutParameter(1, Type.INT);
			        cs.setString(2, per.getNombre());
			        cs.setString(3, per.getApePat());
					cs.setString(4, per.getApeMat());
					cs.setString(5, per.getDireccion());
					cs.setString(6, per.getTel1());
					cs.setString(7, per.getTel2());
					cs.execute();
     				estado = cs.getInt(1);
					cs.close();
					cn.close();
					System.out.print("OK SET PERSONA");
			    }
			catch (SQLException e) {
				System.out.print("ERROR FUE "+e.getMessage());
				}
	}
	
	public void setLocalPersona(int idpersona,int idlocal) throws IOException, ClassNotFoundException, Exception
	{
			CallableStatement cs;
			Connection cn;
			cs = null;
			int estado=0;
			
			try {
	    			System.out.print("BEGIN SET PERSONA");
					cn = new conexion().get_conection();
					String sentence ="{ ? = call \"siscobsms\".\"setLocalPersona\"(?,?) }";  
					cs = cn.prepareCall(sentence);
     		        cs.registerOutParameter(1, Type.INT);
			        cs.setInt(2, idpersona);
			        cs.setInt(3, idlocal);
					cs.execute();
     				estado = cs.getInt(1);
					cs.close();
					cn.close();
					System.out.print("OK SET PERSONA");
			    }
			catch (SQLException e) {
				System.out.print("ERROR FUE "+e.getMessage());
				}
	}

	public ArrayList getPersonas() throws Exception {

		CallableStatement cs;
		Connection cn;
		cs = null;
		cn = null;
		System.out.print("\n Antes de while datos personas -1");
		cn =  new conexion().get_conection();
		ResultSet rs = null;
		ArrayList personas = new ArrayList();

		try {
			System.out.print("\n Antes de while datos personas 0.....pp");
			String sentence ="select * from  \"siscobsms\".\"getPersonas\"()";
			System.out.print("\n Query es "+sentence);
			cs = cn.prepareCall(sentence);
			cs.execute();
			rs = cs.getResultSet();
			persona per;
			System.out.print("\n Antes de while datos personas 1");
			while (rs.next()) {
				per = new persona(); 
				per.setId(rs.getInt("idpersona"));
				per.setNombre(rs.getString("nombres"));
				per.setApePat(rs.getString("apepat")); 
				per.setApeMat(rs.getString("apemat"));
				per.setDireccion(rs.getString("direccion"));
				per.setTel1(rs.getString("telefono1"));
				per.setTel2(rs.getString("telefono2"));
				personas.add(per);
				//System.out.print("\n Dato persona es :"+per.getId()+" / "+per.getNombre()+" / "+per.getApePat()+" / "+per.getApeMat()+" / "+per.getDireccion()+" / "+per.getTel1()+" / "+per.getTel2());
			}

		} catch (Exception e) {
			
			System.out.print("\n Error fue"+e.getMessage());

		}

		cs.close();
		cn.close();

		return personas;
	}
	

}